channelBal = input.bool(false, "Channel Balance")

[middleKC1, upperKC1, lowerKC1]    =   ta.kc(close,   80,   10.5)
[middleKC2, upperKC2, lowerKC2]    =   ta.kc(close,   80,   9.5)
[middleKC3, upperKC3, lowerKC3]    =   ta.kc(close,   80,   8)
[middleKC4, upperKC4, lowerKC4]    =   ta.kc(close,   80,   3)
// Colors
green3 = #5bcd04e4
red3      = #DD0000

// Plots
k1 = plot(ta.ema(upperKC1, 50), "", na, editable=false)
k2 = plot(ta.ema(upperKC2, 50), "", na, editable=false)
k3 = plot(ta.ema(upperKC3, 50), "", na, editable=false)
k4 = plot(ta.ema(upperKC4, 50), "", na, editable=false)
k5 = plot(ta.ema(lowerKC4, 50), "", na, editable=false)
k6 = plot(ta.ema(lowerKC3, 50), "", na, editable=false)
k7 = plot(ta.ema(lowerKC2, 50), "", na, editable=false)
k8 = plot(ta.ema(lowerKC1, 50), "", na, editable=false)
fill(k1, k2, channelBal ? color.new(red3, 40) : na, editable=false)
fill(k2, k3, channelBal ? color.new(red3, 65) : na, editable=false)
fill(k3, k4, channelBal ? color.new(red3, 90) : na, editable=false)
fill(k5, k6, channelBal ? color.new(green3, 90) : na, editable=false)
fill(k6, k7, channelBal ? color.new(green3, 65) : na, editable=false)
fill(k7, k8, channelBal ? color.new(green3, 40) : na, editable=false)
//@version=5
indicator("SimpleAlgo v3.1", overlay=true, precision=0,explicit_plot_zorder=true, max_labels_count=500)


// Get user input
emaCloud         = input.bool(false, "EMA Cloud?")
volCloud        = input.bool(false, "Volatility Cloud?")
volBands        = input.bool(false, "Volatility Bands?")
volSen          = input.float(3.5, "Volatility Sensitivity (1-5 (Half Allowed))", 0.5, 9, 0.5)
signals      = input.bool(true, "Buy/Sell Signals?")
levels       = input.bool(true, "TP/SL Levels?     ", inline="levels")
lvlLines     = input.bool(true, "Show Lines? ", inline="levels")
linesStyle = input.string("SOLID", "", ["SOLID", "DASHED","DOTTED"], inline="levels")
lvlDistance = input.int(1, "Distance", 1, inline="levels2")
lvlDecimals = input.int(2, "        Decimals", 1, 8, inline="levels2")
suppRes        = input.bool(false, "Support/Resistance?")
atrLen        = input.int(14, "ATR Length", 1)
atrRisk      = input.int(1, "ATR/ Risk", 1)
candlesT      = input.bool(true, "Trending Candles")
volBandsSen = input.int(5, "Vol Bands Sensitivity (Default: 5.0)", 1)
useEma         = input.bool(true, "Use Exponential MA?")
barsLR        = input.int(35, "S/R Looking Period", 1)
// Get Components
ema1           = ta.ema(ohlc4, int(10*volSen*2))
ema2           = ta.ema(ohlc4, int(15*volSen*2))
ema3           = ta.ema(ohlc4, int(31*volSen*2))
ema4           = ta.ema(ohlc4, int(34*volSen*2))
ema5           = ta.ema(ohlc4, int(50*volSen*2))
f_kc(src, len, mult) => 
    float basis = useEma ? ta.ema(src, len) : ta.sma(src, len)
    float span = useEma ? ta.ema(ta.tr, len) : ta.sma(ta.tr, len)
    [basis + span * mult, basis - span * mult]



bull = ema3 >= ema3[1] and ema1 >= ema2 and ema1[1] < ema2[1]
bear = ema3 <= ema3[1] and ema1 <= ema2 and ema1[1] > ema2[1]
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = bull ? 1 : 0
atrBand = ta.atr(atrLen) * atrRisk
atrStop = trigger == 1 ? low - atrBand : high + atrBand
barsL     = barsLR
barsR      = barsLR
pivotHigh = fixnan(ta.pivothigh(barsL, barsR)[1])
pivotLow = fixnan(ta.pivotlow(barsL, barsR)[1])
decimals = lvlDecimals == 1 ? "#.#" : lvlDecimals == 2 ? "#.##" : lvlDecimals == 3 ? "#.###" : lvlDecimals == 4 ? "#.####" : lvlDecimals == 5 ? "#.#####" : lvlDecimals == 6 ? "#.######" : lvlDecimals == 7 ? "#.#######" : "#.########"
// Colors
green =               #00CC00       , green5 = volCloud ? color.new(#00CC00, 95) : na, green12_5 = volCloud ? color.new(#00CC00, 87.5) : na, green20 = emaCloud ? #00cc0075 : na
red      =           #CC0000       , red5       = volCloud ? color.new(#CC0000, 95) : na, red12_5 = volCloud ? color.new(#CC0000, 87.5) : na, red20 = emaCloud? color.rgb(204, 0, 0, 62) : na
orange =              #FF9800      , orange50 = emaCloud ? color.new(orange, 50) : na
gray = volBands ? color.rgb(6, 144, 249, 72) : na, gray40 = volBands ? color.new(gray, 60) : na,     gray5      = volBands ? color.new(gray, 95) : na,       gray20 = volBands ? color.new(gray, 80) : na
// Plots
p1 = plot(ema1, "", orange50, editable=false)
p2 = plot(ema2, "", orange50, editable=false)
p3 = plot(ema3, "", orange50, editable=false)
p4 = plot(ema4, "", na, editable=false)
p5 = plot(ema5, "", na, editable=false)
fill(p4, p5, ema4 >= ema5 ? green5 : red5)
fill(p3, p4, ema3 >= ema4 ? green12_5 : red12_5)
fill(p2, p3, ema3 >= ema3[1] ? green20 : red20)
fill(p1, p2, ema1 >= ema3 ? green20 : red20)
barcolor(candlesT ? (ema3 >= ema3[1] ? green : red) : na)
b1 = plot(upperKC1, "", gray40, editable=false)
b2 = plot(upperKC2, "", gray40, editable=false)
b3 = plot(upperKC3, "", gray40, editable=false)
b4 = plot(lowerKC1, "", gray40, editable=false)
b5 = plot(lowerKC2, "", gray40, editable=false)
b6 = plot(lowerKC3, "", gray40, editable=false)
fill(b1, b2, gray5)
fill(b2, b3, gray20)
fill(b4, b5, gray5)
fill(b5, b6, gray20)
plot(pivotHigh, "Resistance", not suppRes or ta.change(pivotHigh) ? na : red, 3, offset=-(barsR + 1), editable=false)
plot(pivotLow, "Support", not suppRes or ta.change(pivotLow) ? na : green, 3, offset=-(barsR + 1), editable=false)
y1 = low - (ta.atr(30) * 2)
y2 = high + (ta.atr(30) * 2)
buy = signals and bull ? label.new(bar_index, y1, ema4 >= ema5 ? "Buy Signal▲" : "Buy Signal▲", xloc.bar_index, yloc.price, #00CC00, label.style_label_up, #141923, size.normal) : na
sell = signals and bear ? label.new(bar_index, y2, ema4 <= ema5 ? "Sell Signal▼" : "Sell Signal▼", xloc.bar_index, yloc.price, #CC0000, label.style_label_down, color.white, size.normal) : na
lastTrade(src) => ta.valuewhen((ema3 >= ema3[1] and ema1 >= ema2 and ema1[1] < ema2[1]) or (ema3 <= ema3[1] and ema1 <= ema2 and ema1[1] > ema2[1]), src, 0)
entry = levels ? label.new(time, close, "Entry: " + str.tostring(lastTrade(close), decimals), xloc.bar_time, yloc.price, color.gray, label.style_label_left, color.white, size.normal) : na
label.set_x(entry, label.get_x(entry) + math.round(ta.change(time) * lvlDistance))
label.set_y(entry, lastTrade(close))
label.delete(entry[1])
stop_y = lastTrade(atrStop)
stop = levels ? label.new(time, close, "Stop Loss: " + str.tostring(stop_y, decimals), xloc.bar_time, yloc.price, #CC0000, label.style_label_left, color.white, size.normal) : na
label.set_x(stop, label.get_x(stop) + math.round(ta.change(time) * lvlDistance))
label.set_y(stop, stop_y)
label.delete(stop[1])
tp1_y = (lastTrade(close)-lastTrade(atrStop))*1 + lastTrade(close)
tp1 = levels ? label.new(time, close, "Take Profit 1: " + str.tostring(tp1_y, decimals), xloc.bar_time, yloc.price, #00CC00, label.style_label_left, color.white, size.normal) : na
label.set_x(tp1, label.get_x(tp1) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp1, tp1_y)
label.delete(tp1[1])
tp2_y = (lastTrade(close)-lastTrade(atrStop))*2 + lastTrade(close)
tp2 = levels ? label.new(time, close, "Take Profit 2: " + str.tostring(tp2_y, decimals), xloc.bar_time, yloc.price, #00CC00, label.style_label_left, color.white, size.normal) : na
label.set_x(tp2, label.get_x(tp2) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp2, tp2_y)
label.delete(tp2[1])
tp3_y = (lastTrade(close)-lastTrade(atrStop))*3 + lastTrade(close)
tp3 = levels ? label.new(time, close, "Take Profit 3: " + str.tostring(tp3_y, decimals), xloc.bar_time, yloc.price, #00CC00, label.style_label_left, color.white, size.normal) : na
label.set_x(tp3, label.get_x(tp3) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp3, tp3_y)
label.delete(tp3[1])
style = linesStyle == "SOLID" ? line.style_solid : linesStyle == "DASHED" ? line.style_dashed : line.style_dotted
lineEntry = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), lastTrade(close), bar_index + lvlDistance, lastTrade(close), xloc.bar_index, extend.none, color.gray, style, 2) : na, line.delete(lineEntry[1])
lineStop = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), stop_y, bar_index + lvlDistance, stop_y, xloc.bar_index, extend.none, #CC0000, style, 2) : na, line.delete(lineStop[1])
lineTp1 = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp1_y, bar_index + lvlDistance, tp1_y, xloc.bar_index, extend.none, #00CC00, style, 2) : na, line.delete(lineTp1[1])
lineTp2 = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp2_y, bar_index + lvlDistance, tp2_y, xloc.bar_index, extend.none, #00CC00, style, 2) : na, line.delete(lineTp2[1])
lineTp3 = levels and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp3_y, bar_index + lvlDistance, tp3_y, xloc.bar_index, extend.none, #00CC00, style, 2) : na, line.delete(lineTp3[1])
// Alerts
alertcondition(bull, "Buy", "SimpleAlgo Community\nBuy {{ticker}} @{{close}}")
alertcondition(bull and ema4 >= ema5, "Firm Buy", "SimpleAlgo Community\nFirm Buy {{ticker}} @ {{close}}")
alertcondition(bear and ema4 <= ema5, "Firm Sell", "SimpleAlgo Community\nFirm Sell {{ticker}} @ {{close}}")
alertcondition(bear, "Sell", "SimpleAlgo Community\nSell {{ticker}} @{{close}}")